package br.com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.connection.ConnectionFactory;
import br.com.controller.FuncionarioController;
import br.com.utils.FormatUtils;

/**
 * Servlet implementation class FuncionarioServlet
 */
@WebServlet("/FuncionarioServlet")
public class FuncionarioServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public FuncionarioServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		try{
			doInser(request, response);
		}catch (Exception e){
			out.println("ocorreu algum erro");
		}

	}

	protected void doInser(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PrintWriter out = response.getWriter();

		request.setCharacterEncoding("UTF-8");        
		response.setCharacterEncoding("UTF-8");
		String nome = request.getParameter("nomefunc");
		String pai = request.getParameter("pai");
		String mae = request.getParameter("mae");
		Date nascimento = null;
		String sexo = request.getParameter("sexo");
		int estadonasc = Integer.parseInt(request.getParameter("estado"));
		int cidadenasc = Integer.parseInt(request.getParameter("cidade"));
		String rg = request.getParameter("rg");
		String cpf = request.getParameter("cpf");
		String end = request.getParameter("endereco");
		String bairro = request.getParameter("bairro");
		int estadoend = Integer.parseInt(request.getParameter("estadoend"));
		int cidadeend = Integer.parseInt(request.getParameter("cidadeend"));
		int funcao = Integer.parseInt(request.getParameter("funcao"));
		String tel = request.getParameter("telefone");
		String cel = request.getParameter("celular");
		//Date datacad = FormatUtils.setData(request.getParameter("datacad"));
		Date datacad = null;
		java.util.Date data = new java.util.Date();
		try {
			nascimento = FormatUtils.setData(request.getParameter("datanasc"));
			datacad = FormatUtils.setDataSql(data);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		Connection conn = null;

		try {

			conn = new ConnectionFactory().getConnection();
			FuncionarioController.inserir(conn, nome, mae, pai, nascimento, sexo, estadonasc, cidadenasc, rg, cpf, end, bairro, estadoend, cidadeend, funcao, tel, cel, datacad);

			out.print("<script type=\"text/javascript\">	alert(\"Cadastrado com sucesso.\");</script>");
			out.print("<meta http-equiv='refresh' content='0;url=/WebTecEscola/Admin.jsp'>");
			conn.commit();

		} catch (Exception e) {
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (Exception ex) {ex.printStackTrace();}
		} finally {
			try {
				conn.close();
			} catch (Exception ex) {}
		} 

	}

}
